package tienda.zapateria.siszap.da;

import java.sql.Connection;
import tienda.zapateria.siszap.be.BECarrito;
import java.util.ArrayList;
import org.rx.cr.dao.DAOAbstract;

public class DACarrito extends DAOAbstract<BECarrito> {

    public DACarrito(Connection c) {
        setConnection(c);
    }

    @Override
    public int registrar(BECarrito be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_ins_carrito");
            setParameterInt(be.getPersonaid());
            setParameterDate(be.getFecha());
            setParameterInt(be.getUbigeoid());
            setParameterString(be.getCliente());
            setParameterString(be.getNumero());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BECarrito be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_upd_carrito");
            setParameterInt(be.getCarritoid());
            setParameterInt(be.getPersonaid());
            setParameterDate(be.getFecha());
            setParameterInt(be.getUbigeoid());
            setParameterString(be.getCliente());
            setParameterString(be.getNumero());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int eliminarRegistro(BECarrito be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_del_carrito");
            setParameterInt(be.getCarritoid());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public ArrayList<BECarrito> buscarReferencia(String string) throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_carrito");
            setParameterString(string);
            commitc();
            while (existResult()) {
                BECarrito be = new BECarrito();
                be.setCarritoid(getDataInt(1));
                be.setPersonaid(getDataInt(2));
                be.setFecha(getDataDate(3));
                be.setUbigeoid(getDataInt(4));
                be.setCliente(getDataString(5));
                be.setNumero(getDataString(6));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public ArrayList<BECarrito> listar() throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_carrito");
            setParameterString("");
            commitc();
            while (existResult()) {
                BECarrito be = new BECarrito();
                be.setCarritoid(getDataInt(1));
                be.setPersonaid(getDataInt(2));
                be.setFecha(getDataDate(3));
                be.setUbigeoid(getDataInt(4));
                be.setCliente(getDataString(5));
                be.setNumero(getDataString(6));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public BECarrito buscar(int i) throws Exception {
        try {
            BECarrito be = null;
            clear();
            setStoreProcedure("zapateria.sp_sel_carrito_id");
            setParameterInt(i);
            commitc();
            while (existResult()) {
                be = new BECarrito();
                be.setCarritoid(getDataInt(1));
                be.setPersonaid(getDataInt(2));
                be.setFecha(getDataDate(3));
                be.setUbigeoid(getDataInt(4));
                be.setCliente(getDataString(5));
                be.setNumero(getDataString(6));
            }
            return be;
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    public int obtenerUltimoId() throws Exception {
        BECarrito be = new BECarrito();
        try {
            setStoreProcedure("sp_obtenercarritoid");
            while (existResult()) {
                be.setCarritoid(getDataInt(1));
            }

            return Integer.parseInt(commit().toString());

        } catch (Exception e) {
            return rollback(e);
        }
    }
}
